import PollingUtil from "/utils/PollingUtil";
Component({
  mixins: [],
  data: {
    intervalOptions: [], // 刷新选项列表
    selectedInterval: '0', // 当前选中值（毫秒字符串）
    countdown: 0, // 倒计时秒数
    showCountdown: false, // 是否显示倒计时
  },
  props: {
    select1: {
      type: Number,
      value: 5
    },
    select2: {
      type: Number,
      value: 10
    },
    select3: {
      type: Number,
      value: undefined,
    },
    // 初始选中值（秒，0表示关闭）
    defaultSelect: {
      type: Number,
      value: 5
    },
    onIntervalChange: () => {},
    onRefresh: () => {}
  },
  didMount() {
    this.initOptions();

    this.initPollingUtil();
  },
  didUpdate() {},
  didUnmount() {
    if (this.polling) {
      this.polling.stop();
    }
  },
  methods: {
    initOptions() {
      const {
        select1,
        select2,
        select3,
        defaultSelect
      } = this.props
      console.error(`获取轮询值：select1:${select1};select2:${select2};select3:${select3};defaultSelect:${defaultSelect}`)

      const options = [{
          value: '0',
          label: '关闭'
        },
        {
          value: `${select1 * 1000}`,
          label: `${select1}秒`
        },
        {
          value: `${select2 * 1000}`,
          label: `${select2}秒`
        }
      ];
      if (select3 && (typeof select3) === 'number') {
        options.push({
          value: `${select3 * 1000}`,
          label: `${select3}秒`
        })
      }

      // 设置初始选中值（转换为毫秒字符串）
      const initialValue = defaultSelect === 0 ?
        '0' :
        `${defaultSelect * 1000}`;

      this.setData({
        intervalOptions: options,
        selectedInterval: initialValue,
        showCountdown: initialValue !== '0'
      });
    },
    onChange(checked, e) {
      console.log(checked);
    },

    // 初始化轮询工具
    initPollingUtil() {
      const {
        defaultSelect
      } = this.props;

      const initialInterval = defaultSelect > 0 ? defaultSelect * 1000 : 5000;
      console.error(`初始化轮询间隔：${initialInterval}`)
      this.polling = new PollingUtil({
        interval: initialInterval,
        callback: this.handlePoll.bind(this),
        onCountdownUpdate: (remainingTime) => {
          this.setData({
            countdown: remainingTime
          });
        }
      });

      // 如果初始不是关闭状态，启动轮询
      if (defaultSelect > 0) {
        this.polling.start();
      }
    },

    // 选项变更处理
    onIntervalChange(e) {
      const newInterval = e.detail.value;
      const isEnabled = newInterval !== '0';

      this.setData({
        selectedInterval: newInterval,
        showCountdown: isEnabled
      });

      // 更新轮询状态
      if (isEnabled) {
        this.polling.setInterval(parseInt(newInterval));
      } else {
        this.polling.stop();
      }
      this.props.onIntervalChange({
        enabled: isEnabled,
        interval: isEnabled ? parseInt(newInterval) / 1000 : 0
      })
      // this.triggerEvent('intervalChange', {
      //   enabled: isEnabled,
      //   interval: isEnabled ? parseInt(newInterval) / 1000 : 0
      // });
    },

    // 轮询执行函数
    handlePoll() {
      this.props.onRefresh({
        autoRefreshInterval: 'hello'
      })
      // this.triggerEvent('refresh', {});
    }
  },
});